package com.sec.android.app.myfiles.thumbnail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;

import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

public class AudioThumbnailImp extends ThumbnailImp {
    public AudioThumbnailImp(Context context) {
        super(context);
    }

    @Override
    protected boolean isSupport(FileRecord record) {
        boolean bRet = false;
        if (!FileType.isMIDFileType(record.getFileType())) {
            bRet = FileType.isAudioFileType(record.getFileType());
        }

        return bRet;
    }

    @Override
    public Bitmap _createThumbnail(FileRecord record, int thumbnailSize) {
        Bitmap retBmp = null;

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        try {
            mmr.setDataSource(record.getFullPath());
            byte[] embedPic = mmr.getEmbeddedPicture();
            if (embedPic != null) {
                BitmapFactory.Options options = getBmpFactoryOption(thumbnailSize);
                retBmp = BitmapFactory.decodeByteArray(embedPic, 0, embedPic.length, options);
            }
        } catch (Exception e) {
            Log.e(this, "Exception:" + e.toString());
        } finally {
            mmr.release();
        }
        return retBmp;
    }

    private BitmapFactory.Options getBmpFactoryOption(int thumbnailSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.outHeight = thumbnailSize;
        options.outWidth = thumbnailSize;
        options.inPreferQualityOverSpeed = false;
        //options.semIsPreview = true;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;

        return options;
    }
}
